//
//  AirspaceOverLayer.m
//  SDK开发版本
//
//  Created by 北京国泰北斗科技有限公司 on 2016/11/14.
//  Copyright © 2016年 北京国泰北斗科技有限公司. All rights reserved.
//


#import "AirspaceOverLayer.h"
#import "UCWMSTileLayer.h"
#import "UCWMSParameter.h"
#import "UCMapView.h"
#import "NSString+Extension.h"

static NSString * const kAirportURL = @"https://api.aeromap.cn/layerws/aeromap/wms";
static NSString * const kTitleLayerURL = @"https://api.aeromap.cn/layerws/aeromap/wms";
static NSString * const kGoogleChinaMap = @"GoogleChinaMap";
static NSString * const kGoogleChinaTerrainMap = @"GoogleChinaTerrainMap";
static NSString * const kGoogleChinaHybridMap = @"GoogleChinaHybridMap";
static NSString * const kUcloudAirportName = @"aeromap:airport";
static NSString * const kUcloudAirportMH = @"airport_type=1";
static NSString * const kUcloudAirportHL = @"airport_type=2";
static NSString * const kUcloudAirportNH = @"airport_type=3";
static NSString * const kUcloudAirportTH = @"airport_type=4";
static NSString * const kUcloudAirportJY = @"airport_type=5";
static NSString * const kUcloudAirportJM = @"airport_type=6";
static NSString * const kUcloudAirportZSJ = @"airport_type=7";
static NSString * const kUcloudAirportJB = @"airport_type=8";
static NSString * const kWmsTitleLayerRestrictedName = @"aeromap:restrictedarea";
static NSString * const kWmsTitleLayerRestrictedFilter = @"level='R'";
static NSString * const kWmsTitleLayerNoFlyName = @"aeromap:restrictedarea";
static NSString * const kWmsTitleLayerFreeZone = @"aeromap:freezone";
static NSString * const kWmsTitleLayerNoFlyFilter = @"level='P'";
static NSString * const kWmsTitleLayerDangerousName = @"aeromap:restrictedarea";
static NSString * const kWmsTitleLayerDangerousFilter = @"level='D'";
static NSString * const kWmsTitleLayerForbiddenName = @"aeromap:forbiddenarea";

@interface AirspaceOverLayer()

@property(nonatomic,strong)UCMapView *mapView;
@property(nonatomic,strong)NSMutableDictionary *layerDic;
@end

@implementation AirspaceOverLayer

+ (instancetype)overLayerWithMapView:(UCMapView *)mapView{
    return [[self alloc] initWithMapView:mapView];
}

- (instancetype)initWithMapView:(UCMapView *)mapView{
    self = [super init];
    if (self) {
        self.mapView = mapView;
        self.layerDic = [NSMutableDictionary new];
    }
    return self;
}

- (void)addOverLayerWithlayerName:(AirspaceOverLayerName)layerName{
    if(layerName & AirspaceOverLayerNameRestricted){
        UCWMSParameter *paramter = [[UCWMSParameter alloc] init];
        paramter.url = kTitleLayerURL;
        paramter.name = [paramter stringUTF8:kWmsTitleLayerRestrictedName];
        paramter.filter = [paramter stringUTF8:kWmsTitleLayerRestrictedFilter];
        [self addWmsTitleLayer:paramter];
    }
    if (layerName & AirspaceOverLayerNameNoFly){
        UCWMSParameter *paramter = [[UCWMSParameter alloc] init];
        paramter.url = kTitleLayerURL;
        paramter.name = [paramter stringUTF8:kWmsTitleLayerNoFlyName];
        paramter.filter = [paramter stringUTF8:kWmsTitleLayerNoFlyFilter];
        [self addWmsTitleLayer:paramter];
    }
    if (layerName & AirspaceOverLayerNameDangerous){
        UCWMSParameter *paramter = [[UCWMSParameter alloc] init];
        paramter.url = kTitleLayerURL;
        paramter.name = [paramter stringUTF8:kWmsTitleLayerDangerousName];
        paramter.filter = [paramter stringUTF8:kWmsTitleLayerDangerousFilter];
        [self addWmsTitleLayer:paramter];
    }
    if (layerName & AirspaceOverLayerNameForbidden){
        UCWMSParameter *paramter = [[UCWMSParameter alloc] init];
        paramter.url = kTitleLayerURL;
        paramter.name = [paramter stringUTF8:kWmsTitleLayerForbiddenName];
        [self addWmsTitleLayer:paramter];
    }
    if (layerName & AirspaceOverLayerNameAirportMH){
        UCWMSParameter *paramter = [[UCWMSParameter alloc] init];
        paramter.url = kAirportURL;
        paramter.name = kUcloudAirportName;
        paramter.filter = kUcloudAirportMH;
        paramter.layerKey = kUcloudAirportMH;
        [self addWmsTitleLayer:paramter];
    }
    if (layerName & AirspaceOverLayerNameAirportHL){
        UCWMSParameter *paramter = [[UCWMSParameter alloc] init];
        paramter.url = kAirportURL;
        paramter.name = kUcloudAirportName;
        paramter.filter = kUcloudAirportHL;
        paramter.layerKey = kUcloudAirportHL;
        [self addWmsTitleLayer:paramter];
    }
    if (layerName & AirspaceOverLayerNameAirportNH){
        UCWMSParameter *paramter = [[UCWMSParameter alloc] init];
        paramter.url = kAirportURL;
        paramter.name = kUcloudAirportName;
        paramter.filter = kUcloudAirportNH;
        paramter.layerKey = kUcloudAirportNH;
        [self addWmsTitleLayer:paramter];
    }
    if (layerName & AirspaceOverLayerNameAirportTH){
        UCWMSParameter *paramter = [[UCWMSParameter alloc] init];
        paramter.url = kAirportURL;
        paramter.name = kUcloudAirportName;
        paramter.filter = kUcloudAirportTH;
        paramter.layerKey = kUcloudAirportTH;
        [self addWmsTitleLayer:paramter];
    }
    if (layerName & AirspaceOverLayerNameAirportJY){
        UCWMSParameter *paramter = [[UCWMSParameter alloc] init];
        paramter.url = kAirportURL;
        paramter.name = kUcloudAirportName;
        paramter.filter = kUcloudAirportJY;
        paramter.layerKey = kUcloudAirportJY;
        [self addWmsTitleLayer:paramter];
    }
    if (layerName & AirspaceOverLayerNameAirportJM){
        UCWMSParameter *paramter = [[UCWMSParameter alloc] init];
        paramter.url = kAirportURL;
        paramter.name = kUcloudAirportName;
        paramter.filter = kUcloudAirportJM;
        paramter.layerKey = kUcloudAirportJM;
        [self addWmsTitleLayer:paramter];
    }
    if (layerName & AirspaceOverLayerNameAirportZSJ){
        UCWMSParameter *paramter = [[UCWMSParameter alloc] init];
        paramter.url = kAirportURL;
        paramter.name = kUcloudAirportName;
        paramter.filter = kUcloudAirportZSJ;
        paramter.layerKey = kUcloudAirportZSJ;
        [self addWmsTitleLayer:paramter];
    }
    if (layerName & AirspaceOverLayerNameAirportJB){
        UCWMSParameter *paramter = [[UCWMSParameter alloc] init];
        paramter.url = kAirportURL;
        paramter.name = kUcloudAirportName;
        paramter.filter = kUcloudAirportJB;
        paramter.layerKey = kUcloudAirportJB;
        [self addWmsTitleLayer:paramter];
    }
    if (layerName & AirspaceOverLayerNameFreeZone){
        UCWMSParameter *paramter = [[UCWMSParameter alloc] init];
        paramter.url = kTitleLayerURL;
        paramter.name = kWmsTitleLayerFreeZone;
        paramter.layerKey = kWmsTitleLayerFreeZone;
        [self addWmsTitleLayer:paramter];
    }
}
- (void)addWmsTitleLayer:(UCWMSParameter *)paramter{
    paramter.mapView = self.mapView;
    if ([_layerDic objectForKey:paramter.layerKey] == nil) {
        UCWMSTileLayer *wmsTileLayer = [[UCWMSTileLayer alloc] initWithLayerParameter:paramter];
        [self.layerDic setValue:wmsTileLayer forKey:paramter.layerKey];
        wmsTileLayer.openCache = _mapView.openCache;
        wmsTileLayer.tileSize = 512;
        wmsTileLayer.zIndex = 3;
        wmsTileLayer.titleLayerType = WMSTitleLayerOverlay;
        wmsTileLayer.map = [_mapView valueForKey:@"map"];
    }
}

- (NSString *)stringUTF8:(NSString *)layerName withFilter:(NSString *)filter{
    
    layerName = [NSString encodeToPercentEscapeString:layerName];
    filter = [NSString encodeToPercentEscapeString:filter];
    
    return [NSString stringWithFormat:@"%@-%@",layerName,filter];
}

- (void)deleteOverLayerWithLayerName:(AirspaceOverLayerName)layerName{
    
    NSString *layerKey = @"需要删除的图层";
    if (layerName & AirspaceOverLayerNameRestricted) {
        layerKey = [self stringUTF8:kWmsTitleLayerRestrictedName withFilter:kWmsTitleLayerRestrictedFilter];
        [self _removeLayerWithLayerName:layerKey];
    }
    if (layerName & AirspaceOverLayerNameNoFly) {
        layerKey = [self stringUTF8:kWmsTitleLayerNoFlyName withFilter:kWmsTitleLayerNoFlyFilter];
        [self _removeLayerWithLayerName:layerKey];
    }
    if (layerName & AirspaceOverLayerNameDangerous) {
        layerKey = [self stringUTF8:kWmsTitleLayerDangerousName withFilter:kWmsTitleLayerDangerousFilter];
        [self _removeLayerWithLayerName:layerKey];
    }
    if (layerName & AirspaceOverLayerNameForbidden) {
        layerKey = [self stringUTF8:kWmsTitleLayerForbiddenName withFilter:nil];
        [self _removeLayerWithLayerName:layerKey];
    }
    if (layerName & AirspaceOverLayerNameAirportMH) {
        layerKey = kUcloudAirportMH;
        [self _removeLayerWithLayerName:layerKey];
    }
    if (layerName & AirspaceOverLayerNameAirportHL) {
        layerKey = kUcloudAirportHL;
        [self _removeLayerWithLayerName:layerKey];
    }
    if (layerName & AirspaceOverLayerNameAirportNH) {
        layerKey = kUcloudAirportNH;
        [self _removeLayerWithLayerName:layerKey];
    }
    if (layerName & AirspaceOverLayerNameAirportTH) {
        layerKey = kUcloudAirportTH;
        [self _removeLayerWithLayerName:layerKey];
    }
    if (layerName & AirspaceOverLayerNameAirportJY) {
        layerKey = kUcloudAirportJY;
        [self _removeLayerWithLayerName:layerKey];
    }
    if (layerName & AirspaceOverLayerNameAirportJM) {
        layerKey = kUcloudAirportJM;
        [self _removeLayerWithLayerName:layerKey];
    }
    if (layerName & AirspaceOverLayerNameAirportZSJ) {
        layerKey = kUcloudAirportZSJ;
        [self _removeLayerWithLayerName:layerKey];
    }
    if (layerName & AirspaceOverLayerNameAirportJB) {
        layerKey = kUcloudAirportJB;
        [self _removeLayerWithLayerName:layerKey];
    }
    if (layerName & AirspaceOverLayerNameFreeZone) {
        layerKey = kWmsTitleLayerFreeZone;
        [self _removeLayerWithLayerName:layerKey];
    }
}
#pragma mark - 删除图层
- (void)_removeLayerWithLayerName:(NSString *)layerName{
    
    if ([self.layerDic allKeys].count > 0) {
        UCWMSTileLayer *wmsTileLayer = [self.layerDic objectForKey:layerName];
        if ([wmsTileLayer.layerName isEqualToString:layerName]) {
            [self.layerDic removeObjectForKey:layerName];
            wmsTileLayer.map = nil;
        }
    }
}
@end
